OpMap = {}

def _register_op(op_code, op_name):
    OpMap[op_code] = op_name

'''
STOP

算术运算
ADD SUB MUL DIV

一元运算
NEGATE  (- 1)

逻辑运算
AND
OR
NOT

常量操作
LOAD_CONST


'''

STOP = -1
NOP = 0x00
ADD = 0x01
SUB = 0x02
MUL = 0x03
DIV = 0x04
LOAD_CONST = 0x05
RETURN = 0x06
PRINT = 0x07
PUSH = 0x08
POP = 0x09
CALL = 0x0a
SET_GLOBAL = 0x0b
LOAD_GLOBAL = 0x0c
SET_LOCAL = 0x0d
LOAD_LOCAL = 0x0e
CALL = 0x0d

_register_op(STOP, "STOP")
_register_op(NOP, "NOP")
_register_op(ADD, "ADD")
_register_op(SUB, "SUB")
_register_op(MUL, "MUL")
_register_op(DIV, "DIV")
_register_op(LOAD_CONST, "LOAD_CONST")
_register_op(RETURN, "RETURN")
_register_op(PRINT, "PRINT")
_register_op(PUSH, "PUSH")
_register_op(POP, "POP")
_register_op(CALL, "CALL")
_register_op(SET_GLOBAL, "SET_GLOBAL")
_register_op(LOAD_GLOBAL, "LOAD_GLOBAL")
_register_op(CALL, "CALL")

def disassemble_codes(codes: list[int]):
    print("---Disassemble codes:")
    i = 0
    while i < len(codes):
        op_code = codes[i]
        if op_code == STOP:
            print("STOP")
        elif op_code == LOAD_CONST:
            index = codes[i + 1]
            print(f"LOAD_CONST {index}")
            i += 1
        elif op_code == ADD:
            print("ADD")
        elif op_code == PRINT:
            print("PRINT")
        elif op_code == CALL:
            print("CALL")
        elif op_code == SET_GLOBAL:
            print("SET_GLOBAL")
        elif op_code == LOAD_GLOBAL:
            print("LOAD_GLOBAL")
        elif op_code == RETURN:
            print("RETURN")
        elif op_code == LOAD_LOCAL:
            print("LOAD_LOCAL")
        elif op_code == SET_LOCAL:
            print("SET_LOCAL")

        else:
            print(f"Unknown instruction {OpMap[op_code]}")
        i += 1
    print("---Disassemble end")